package com.key.web_admin.controller;

import com.key.common.base.RespResult;
import com.key.modules.dto.DriverAddDTO;
import com.key.modules.dto.DriverEditDTO;
import com.key.modules.service.DriverService;
import com.key.modules.vo.DriverDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wei
 */
@RestController
@RequestMapping("/driver")
public class DriverController {

    @Autowired
    DriverService driverService;

    /**
     * 查询司机表（支持条件查询）
     * @author wei
     * @param currentPage 当前页码数
     * @param pageSize 每页显示记录数
     * @param dType 类型
     * @param dTelNum 手机号（条件查询时的可选参数）
     * @param dName 司机姓名（条件查询时的可选参数）
     * @param dAuditStatus 审核状态（条件查询时的可选参数）
     * @return
     */
    @GetMapping("/findDriverList")
    public RespResult findDriverList(Integer currentPage, Integer pageSize, String dType,
                                     String dTelNum, String dName, String dAuditStatus) {
        RespResult respResult;
        try{
            respResult = driverService.findDriverList(currentPage, pageSize, dType, dTelNum, dName, dAuditStatus);
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }

        return respResult;
    }

    /**
     * 查询司机详情
     * @author wei
     * @param dId 司机ID
     * @return
     */
    @GetMapping("/findDriverDetails")
    public RespResult findDriverDetails(Integer dId) {
        RespResult respResult;
        try{
            DriverDetailsVO driverDetailsVO = driverService.findDriverDetails(dId);
            respResult = RespResult.success(driverDetailsVO);
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 添加司机
     * @author wei
     * @param driverAddDTO 新增司机时需要填写的参数
     * @return
     */
    @PostMapping("/addDriver")
    public RespResult addDriver(@RequestBody DriverAddDTO driverAddDTO) {
        RespResult respResult;
        try{
            boolean result = driverService.addDriver(driverAddDTO);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 删除司机
     * @author wei
     * @param dId 司机ID
     * @return
     */
    @PostMapping("/removeDriver")
    public RespResult removeDriver(Integer dId) {
        RespResult respResult;
        try{
            boolean result = driverService.removeDriver(dId);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 修改司机
     * @author wei
     * @param driverEditDTO 修改司机时需要填写的参数
     * @return
     */
    @PostMapping("/editDriver")
    public RespResult editDriver(@RequestBody DriverEditDTO driverEditDTO) {
        RespResult respResult;
        try{
            boolean result = driverService.editDriver(driverEditDTO);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 审核司机
     * @author wei
     * @param dId 司机ID
     * @param dAuditStatus 审核状态
     * @return
     */
    @PostMapping("/examineAndVerify")
    public RespResult examineAndVerify(Integer dId, Byte dAuditStatus) {
        RespResult respResult;
        try{
            boolean result = driverService.examineAndVerify(dId, dAuditStatus);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("操作失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }
}
